package org.jeecg.modules.paycenter.controller;

import cn.hutool.core.util.ObjectUtil;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.annotation.RequestLog;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.paycenter.channel.bankdocking.dto.RespPacket;
import org.jeecg.modules.paycenter.channel.bankdocking.utils.BankUtils;
import org.jeecg.modules.paycenter.pay.util.NhBankUtils;
import org.jeecg.modules.system.constant.UserTypeEnum;
import org.jeecg.modules.wlhy.customer.entity.HyCustomerAccountBook;
import org.jeecg.modules.wlhy.customer.service.IHyCustomerAccountBookService;
import org.jeecg.modules.wlhy.hycarrier.entity.HyCarrierAccountBook;
import org.jeecg.modules.wlhy.hycarrier.service.IHyCarrierAccountBookService;
import org.jeecg.modules.wlhy.hydriver.entity.HyDriverAccountBook;
import org.jeecg.modules.wlhy.hydriver.service.IHyDriverAccountBookService;
import org.jeecg.modules.wlhy.utils.CodeUtil;
import org.jeecg.modules.wlhy.utils.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

/**
 * @author yangqf
 * @date 2023/7/3 11:23
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/abc")
public class ABCBankController {
    @Autowired
    private BankUtils bankUtils;

    @Autowired
    private UserUtil userUtil;
    @Autowired
    private IHyCustomerAccountBookService customerAccountBookService;
    @Autowired
    private IHyDriverAccountBookService driverAccountBookService;
    @Autowired
    private IHyCarrierAccountBookService carrierAccountBookService;

    @PostMapping("/balance")
    @RequestLog(description = "余额调整")
    public Result balance(@RequestParam("accNo") String accNo,
                          @RequestParam("prov") String prov,
                          @RequestParam("amt") String amt,
                          @RequestParam("outAccNo") String outAccNo,
                          @RequestParam("inAccNo") String inAccNo) throws Exception {
        // RespPacket payResult = NhBankUtils.adjustAccountBookBalance(accNo, prov, new BigDecimal(amt), outAccNo, inAccNo, "test");
        return Result.OK();
    }



    @PostMapping("/updateAccountName")
    @RequestLog(description = "修改多级账簿名称")
    public Result updateAccountName(@RequestParam("accountName") String accountName,
                                    @RequestParam("id") String id,
                                    @RequestParam("type") String type) throws Exception {
        if(!userUtil.isSys()){
            return Result.error("没有权限！");
        }
        if (ObjectUtil.isEmpty(accountName)) {
            return Result.error("账簿名称不能为空！");
        }

        String logAccNo = "";

        if (type.equals(UserTypeEnum.货主.getCode())) {
            HyCustomerAccountBook accountBook = customerAccountBookService.getAccountBookByCustomerId(id);
            if (ObjectUtil.isEmpty(accountBook)) {
                return Result.error("货主钱包不存在！");
            } else {
                logAccNo = accountBook.getLogAccNo();
            }
        } else if (type.equals(UserTypeEnum.司机.getCode())) {
            HyDriverAccountBook accountBook = driverAccountBookService.getAccountBookByDriverId(id);
            if (ObjectUtil.isEmpty(accountBook)) {
                return Result.error("司机钱包不存在！");
            } else {
                logAccNo = accountBook.getLogAccNo();
            }
        } else if (type.equals(UserTypeEnum.代收人.getCode())) {
            HyCarrierAccountBook accountBook = carrierAccountBookService.getAccountBookByCarrierId(id);
            if (ObjectUtil.isEmpty(accountBook)) {
                return Result.error("司机钱包不存在！");
            } else {
                logAccNo = accountBook.getLogAccNo();
            }
        }
        if (ObjectUtil.isEmpty(logAccNo)) {
            return Result.error("没有找到对应的多级账簿！");
        }
        RespPacket payResult = bankUtils.editAccountBookName(logAccNo, accountName);
        if (!payResult.isSuccess()) {
            return Result.error("修改失败！");
        } else {
            if (type.equals(UserTypeEnum.货主.getCode())) {
                customerAccountBookService.lambdaUpdate()
                        .set(HyCustomerAccountBook::getLogAccName, accountName)
                        .eq(HyCustomerAccountBook::getLogAccNo, logAccNo)
                        .update();
            } else if (type.equals(UserTypeEnum.司机.getCode())) {
                driverAccountBookService.lambdaUpdate()
                        .set(HyDriverAccountBook::getLogAccName, accountName)
                        .eq(HyDriverAccountBook::getLogAccNo, logAccNo)
                        .update();
            } else if (type.equals(UserTypeEnum.代收人.getCode())) {
                carrierAccountBookService.lambdaUpdate()
                        .set(HyCarrierAccountBook::getLogAccName, accountName)
                        .eq(HyCarrierAccountBook::getLogAccNo, logAccNo)
                        .update();
            }
        }
        return Result.OK("修改成功！");
    }

}
